<template>
  <div>
    <div class="head">
      <van-nav-bar>
        <template #left>
          <div class="logo">
            <img src="@/assets/images/home_active.png" alt="" />
            <span class="logo-title">Luckin Coffee</span>
          </div>
        </template>
        <template #right>
          <div class="goBefore" @click="methods.goLook">先逛一逛</div>
        </template>
      </van-nav-bar>
    </div>
    <div class="login-box">
      <h1 class="login-title">找回密码！</h1>
      <p class="login-subTitle">Find password</p>
      <van-form @submit="methods.onSubmit">
        <van-field
          v-model="phone"
          name="手机号"
          label="手机号"
          placeholder="手机号"
          :rules="[
            {
              required: true,
              message: '请填写正确手机号',
              pattern: /^1[3-9][0-9]{9}$/
            }
          ]"
        />
        <van-field
          v-model="password"
          :type="isShowPassword ? 'text' : 'password'"
          name="密码"
          label="密码"
          placeholder="密码"
          :right-icon="isShowPassword ? 'eye-o' : 'closed-eye'"
          @click-right-icon="isShowPassword = !isShowPassword"
          :rules="[
            {
              required: true,
              message: '必须以字母开头,6-16位字母数字下划线组合',
              pattern: /^[A-Za-z]\w{5,15}$/
            }
          ]"
        />
        <div class="login-tip">
          <span @click="methods.goLogin">已有账号,立即登录</span>
        </div>
        <div style="margin: 16px">
          <van-button
            round
            block
            type="primary"
            native-type="submit"
            color="rgb(12,52,186)"
          >
            提交
          </van-button>
        </div>
      </van-form>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="primary"
          native-type="submit"
          color="#777"
          plain
          @click="methods.back"
        >
          返回
        </van-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { postFormData } from '@/api'
import { encryptPassword } from '@/utils/crypto'
import { showSuccessToast, showToast } from 'vant'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const phone = ref('')
const password = ref('')
const isShowPassword = ref(false)

const methods = {
  onSubmit() {
    postFormData({
      url: '/auth/password/reset',
      params: {
        phone: phone.value,
        password: encryptPassword(password.value)
      }
    })
      .then((res) => {
        if (res.data) {
          showSuccessToast('重置密码成功')
          router.push({ name: 'Login' })
        } else {
          showToast('找回密码失败,请输入正确手机号和密码')
        }
      })
      .catch((err) => {
        console.error(err)
      })
  },
  back() {
    router.go(-1)
  },
  goLogin() {
    router.push({ name: 'Login' })
  },
  goLook() {
    router.push({ name: 'Home' })
  }
}

// 判断是否从忘记密码页面跳转过来，且是否通过邮箱校验
</script>

<style lang="less" scoped>
.head {
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 36px;
    }
    .logo-title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .goBefore {
    color: rgb(12, 52, 186);
  }
}
.login-box {
  padding: 0 20px;
  margin-top: 30%;
  .login-title {
    color: #666;
  }
  .login-subTitle {
    color: #666;
    font-size: 16px;
  }
  .login-tip {
    text-align: right;
    font-size: 14px;
    line-height: 30px;
    margin-top: 10px;
    margin-bottom: 40px;
    padding-right: 10px;
  }
  .popup-title {
    color: #666;
    font-size: 24px;
    padding: 10px;
    font-weight: bold;
  }
}
:deep(.van-field__label) {
  margin: 0;
}
</style>
